package com.example.bobo.yuejian322.Activity;

import android.app.FragmentManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bobo.yuejian322.Interfa.MySeverice;
import com.example.bobo.yuejian322.MyImage.CircleImageView;
import com.example.bobo.yuejian322.MyImage.ConsultDialog;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseActivity;
import com.example.bobo.yuejian322.bean.My;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UpdateMyActivity extends BaseActivity {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    private CircleImageView photo;
    TextView  MyName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_my);
        // ImageView images = (ImageView)findViewById(R.id.user_photo);
        ImageView name = (ImageView) findViewById(R.id.user_name);
        ImageView pass = (ImageView) findViewById(R.id.user_pass);
        ImageView back = (ImageView) findViewById(R.id.update_back);
        TextView baocun = (TextView) findViewById(R.id.baocun);
         photo = (CircleImageView) findViewById(R.id.imageView_photo);
          MyName= (TextView) findViewById(R.id.my_name);


        //获取个人信息
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MySeverice mySeverice = retrofit.create(MySeverice.class);

        Call<My> call = mySeverice.my(getUserId(), 191);
        call.enqueue(new Callback<My>() {
            @Override
            public void onResponse(Call<My> call, Response<My> response) {
                My my =response.body();
                if (getApplication() != null) {
                    MyName.setText(my.getUser_name());
                    Object pic=my.getPhoto();

                    if (pic!=null){
                        Picasso.with(getApplication()).load("http://172.24.10.175/workout/Uploads/"+pic).error(R.drawable.image1).into(photo);
                    }
                }
            }
            @Override
            public void onFailure(Call<My> call, Throwable t) {

            }
        });











//调用Dialog修改密码布局
        pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm = getFragmentManager();
                ConsultDialog dialog = new ConsultDialog(UpdateMyActivity.this, R.layout.dialog_pass);
                dialog.show(fm, "showPhotoSelDialog");
            }
        });

//调用Dialog修改用户名布局
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm = getFragmentManager();
                ConsultDialog dialog = new ConsultDialog(UpdateMyActivity.this, R.layout.dialog_name);
                dialog.show(fm, "showPhotoSelDialog");
            }
        });

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    //修改密码接口
    public void Pass(String pass) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MySeverice mySeverice = retrofit.create(MySeverice.class);
        Call<String> call = mySeverice.updatepass(getUserId(), 191, pass);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Toast.makeText(UpdateMyActivity.this, "修改密码成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });


    }

    //修改用户名接口
    public void Name(String name) {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MySeverice mySeverice = retrofit.create(MySeverice.class);
        Call<String> call = mySeverice.updatename(getUserId(), 191, name);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Toast.makeText(UpdateMyActivity.this, "用户名修改成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });


    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("UpdateMy Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}
